﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.IO;
using NOS.Core;
using System.Text;
using NOS.Security;

namespace IISHandle
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }
        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            NameValueCollection Parametros = new NameValueCollection();
            
            NOSCore Nos = new NOSCore();

            if (Context.Request.HttpMethod == "POST")
            {
                Stream Input = Context.Request.InputStream;
                StreamReader Reader =  new StreamReader(Input, Context.Request.ContentEncoding);
                Parametros = HttpUtility.ParseQueryString(Reader.ReadToEnd(),Context.Request.ContentEncoding);
            }
            else
            {
                Parametros = Context.Request.QueryString;
            }
            NameValueCollection Parans = new NameValueCollection();

            for (int i = 0; i < Parametros.Count; i++)
            {
                Parans.Add(Parametros.AllKeys[i].ToString(), Parametros[i]);
            }

            string Nosresponse = "";

            Nosresponse = Nos.Process(Context.Request.Url.AbsolutePath, Parans);
            Context.Response.Write(Nosresponse);
            Context.Response.End();
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Session_End(object sender, EventArgs e)
        {
        }

        protected void Application_End(object sender, EventArgs e)
        {
        }
    }
}